package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.TongjiAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 统计界面
 *
 * @author yingchunli
 */
public class TongjiActivity extends BaseSoocActivity {

    CallNameDao dao;
    LinearLayoutManager linearLayoutManager;//布局管理器
    @InjectView(R.id.list_callnametongji)
    UltimateRecyclerView listTongji;//列表数据
    TongjiAdapter adapter;//适配器
    int maxLastPositon;//分页最后位置
    @InjectView(R.id.blank_view)
    RelativeLayout layout;//空白页布局
    int pageNo=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_tongji);
        dao = new CallNameDao(this);
        ButterKnife.inject(this);
        //初始化listView
        linearLayoutManager = new LinearLayoutManager(this);
        listTongji.setLayoutManager(linearLayoutManager);

        //发送请求，获取列表数据
        PageConst.pageSize = 10;
        dao.getTongjiList(pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TONGJI_FRIST);//获取数据
    }

    /**
     * 初始化数据
     */
    private void initView() {
        adapter = new TongjiAdapter(dao.getClassInfoList(), this);
        listTongji.setAdapter(adapter);
        listTongji.enableLoadmore();
        listTongji.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                dao.getTongjiList(pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TONGJI_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        listTongji.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.getTongjiList(++pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TONGJI_PULLUP);
                } else {
                    if (listTongji != null) {
                        adapter.loadNoDataView();
                    }
                    linearLayoutManager.scrollToPosition(maxLastPositon - 1);
                }
            }
        });
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_GET_TONGJI_FRIST:
                        //获取列表数据 首次请求
                        initView();
                        Log.d("status->" + dao.getStatus());
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            listTongji.hideEmptyView();
                        } else {
                            //无数据处理
                            setBlankView();
                            listTongji.showEmptyView();
                        }

                        break;
                    case CodeConst.EXPAND_GET_TONGJI_PUSHDOWN:
                        listTongji.setRefreshing(false);
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            listTongji.hideEmptyView();
                            adapter.refresh(dao.getClassInfoList());
                        } else {
                            //无数据处理
                            adapter.clear();
                            setBlankView();
                            listTongji.showEmptyView();
                        }
                        if (dao.getClassInfoList().size() > (PageConst.pageSize - 1)) {
                            adapter.loadMoreDataView();
                        } else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    case CodeConst.EXPAND_GET_TONGJI_PULLUP:
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            adapter.loadmore(dao.getClassInfoList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    /**
     * 无数据时，空白页的设置
     */
    void setBlankView() {
        layout.setVisibility(View.VISIBLE);
        TextView textView = (TextView) findViewById(R.id.tipBlank);
        textView.setText("还没有统计数据！");
    }

    @Override
    public String setupToolBarTitle() {
        return "统计";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }
}
